package cn.marinda.resturants.service.impl;

import cn.marinda.resturants.entity.Staff;
import cn.marinda.resturants.entity.Supplier;
import cn.marinda.resturants.entity.User;
import cn.marinda.resturants.global.Pages;
import cn.marinda.resturants.mapper.SupplierMapper;
import cn.marinda.resturants.service.SupplierService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

@Service("supplierService")
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper,Supplier> implements SupplierService {

    @Resource
    private SupplierMapper supplierMapper;

    /**
     * 返回所有供应商
     *
     * @return 供应商
     */
    @Override
    public List<Supplier> queryList() {
        List<Supplier> supplierList = supplierMapper.selectList(null);
        return supplierList;
    }

    /**
     * 选择uid
     *
     * @param uid uid
     * @return {@link Supplier}
     */
    @Override
    public Supplier selectUid(String uid) {
        return supplierMapper.selectById(uid);
    }

    /**
     * 根据菜品的uid来找到对应的供应商
     *
     * @param uid uid
     * @return 供应商
     * @author Marinda
     */
    @Override
    public Supplier selectMealUid(String uid) {
        if(supplierMapper.selectMealUid(uid) == null){
            return null;
        }
        return supplierMapper.selectMealUid(uid);
    }

    /**
     * 模糊查询供货商姓名来找到这个供货商
     *
     * @param name 姓名
     * @return 供货商
     */
    @Override
    public List<Supplier> selectLikeName(String name) {

        return supplierMapper.selectLikeName(name);
    }

    /**
     * 查询指定供应商的单价
     *
     * @param uid uid
     * @return 价格单价
     */
    @Override
    public BigDecimal selectUnitPrice(String uid) {

        return supplierMapper.selectUnitPrice(uid);
    }

    /**
     * 查询指定供应商的总价
     *
     * @param uid uid
     * @return 价格单价
     */
    @Override
    public BigDecimal selectTotalPrice(String uid) {

        return supplierMapper.selectTotalPrice(uid);
    }

    /**
     * 查询指定范围内的所有单价区间供应商
     *
     * @param startUnitPrice 开始单价
     * @param endUnitPrice   结束单价
     * @return 价格单价
     */
    @Override
    public List<Supplier> selectUnitPriceInterval(BigDecimal startUnitPrice, BigDecimal endUnitPrice) {
        return supplierMapper.selectUnitPriceInterval(startUnitPrice, endUnitPrice);
    }

    /**
     * 查询指定范围内的所有价格区间供应商
     *
     * @param startTotalPrice 开始单价
     * @param endTotalPrice   结束单价
     * @return 指定范围内供应商
     */
    @Override
    public List<Supplier> selectTotalPriceInterval(BigDecimal startTotalPrice, BigDecimal endTotalPrice) {
        return supplierMapper.selectTotalPriceInterval(startTotalPrice, endTotalPrice);
    }

    /**
     * 查询指定日期内上次进货时间区间
     *
     * @param startDateFormat 开始日期区间
     * @param endDateFormat   结束日期区间
     * @return 指定范围内供应商
     */
    @Override
    public List<Supplier> selectUpDateInterval(String startDateFormat, String endDateFormat) {

        return supplierMapper.selectUpDateInterval(startDateFormat, endDateFormat);
    }

    /**
     * 查询指定日期内最新进货时间区间
     *
     * @param startDateFormat 开始日期区间
     * @param endDateFormat   结束日期区间
     * @return 指定范围内供应商
     */
    @Override
    public List<Supplier> selectNewDateInterval(String startDateFormat, String endDateFormat) {

        return supplierMapper.selectNewDateInterval(startDateFormat, endDateFormat);
    }

    /**
     * 查询指定上次进货日期的供应商
     *
     * @param dateFormat 日期
     * @return 指定范围内供应商
     */
    @Override
    public Supplier selectUpDateById(String dateFormat) {

        return supplierMapper.selectUpDateById(dateFormat);
    }

    /**
     * 查询指定最新进货日期的供应商
     *
     * @param dateFormat 日期
     * @return 指定范围内供应商
     */
    @Override
    public Supplier selectNewDateById(String dateFormat) {

        return supplierMapper.selectNewDateById(dateFormat);
    }

    /**
     * 添加一名供应商
     *
     * @param supplier 供应商
     * @return 结果
     */
    @Override
    public boolean addSupplier(Supplier supplier) {
        supplier.setUid(UUID.randomUUID().toString());
        return supplierMapper.insert(supplier)>0;
    }


    /**
     * 删除一名供应商
     *
     * @param uid 供应商uid
     * @return 结果
     */
    @Override
    public boolean removeSupplier(String uid) {
        return supplierMapper.deleteById(uid)>0;
    }

    /**
     * 乐观锁修改供应商
     *
     * @param supplier 供应商
     * @return 结果
     */
    @Override
    public boolean updateSupplier(Supplier supplier) {
        Supplier supplier1 = supplierMapper.selectById(supplier.getUid());
        if(supplier1 == null){
            return false;
        }
        supplier.setVersion(supplier1.getVersion());
        return lambdaUpdate().eq(Supplier::getUid,supplier.getUid()).eq(Supplier::getVersion,supplier.getVersion()).set(Supplier::getVersion,supplier.getVersion()+1).update(supplier);
    }

    /**
     * 根据uid查询一名供应商
     *
     * @param uid 供应商uid
     * @return 供应商
     */
    @Override
    public Supplier selectByUid(String uid) {
        return supplierMapper.selectById(uid);
    }

    /**
     * 分页Staff
     *
     * @param pages 传输page数据
     * @return 迭代的page
     */
    @Override
    public IPage<Supplier> selectPages(Pages pages) {
        IPage<Supplier> iPage = supplierMapper.selectPages(pages);
        return iPage;
    }
}
